import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/order_header.dart';
import 'package:boss/models/order_page_data.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';

class OrderProvider extends ChangeNotifier {
  DateTime startTime;

  String get startTimeText {
    return startTime == null ? null : flustars.DateUtil.formatDate(startTime, format: flustars.DateFormats.full);
  }

  DateTime endTime;

  String get endTimeText {
    return endTime == null ? null : flustars.DateUtil.formatDate(endTime, format: flustars.DateFormats.full);
  }

  int page = 1;

  int type;

  List<Order> orders = [];

  int orderCount;

  int orderPrice;

  OrderHeader orderHeader;

  final GlobalKey<LoaderPullRefreshState> loaderPullRefreshKey = GlobalKey<LoaderPullRefreshState>();

  OrderProvider() {
    startTime = DateUtils.startOfTheDay(DateTime.now());
    endTime = DateUtils.endOfTheDay(DateTime.now());
  }

  setType(int type) async {
    this.type = type;
    notifyListeners();

    refresh();
  }

  setStartTime(List<DateTime> dateTime) {
    startTime = dateTime[0];
    endTime = dateTime[1];

    notifyListeners();

    dateRangeChangeHandle();
  }

  Future<Result<List<Order>>> queryOrderListData() async {
    try {
      var response = await api.queryOrderList(
        page: page,
        startTime: startTimeText,
        endTime: endTimeText,
        type: type,
      );
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      orders.addAll(response.data.orderPropPageResult.items);
      orderCount = response.data.num;
      orderPrice = response.data.totalFee;

      notifyListeners();

      return Result.value(response.data.orderPropPageResult.items);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<List<Order>>> refreshOrderList() async {
    page = 1;
    orders = [];

    return queryOrderListData();
  }

  Future<Result<List<Order>>> loadMoreOrderList() async {
    page += 1;

    return queryOrderListData();
  }

  Future<Result<OrderHeader>> queryOrderHeader() async {
    try {
      var response = await api.queryOrderDetailHead(startDate: startTimeText, endDate: endTimeText);
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      orderHeader = response.data;
      if (type == null) {
        type = response.data.orderPropDetailHeadVOList[0].type;
      }
      notifyListeners();

      return Result.value(orderHeader);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<List<Order>>> refreshTask() async {
    await queryOrderHeader();

    return refreshOrderList();
  }

  refresh() {
    loaderPullRefreshKey.currentState.refresh();
  }

  dateRangeChangeHandle() {
    if (startTime != null && endTime != null) refresh();
  }
}
